package com.cloud.wms.base;


import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.cloud.core.base.BasePresenter;
import com.cloud.core.base.BaseView;
import com.cloud.core.eventBus.EventBusUtil;
import com.cloud.core.widget.dialog.LoadingDialog;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.injection.components.DaggerFragmentComponent;
import com.cloud.wms.injection.components.FragmentComponent;
import com.cloud.wms.injection.modules.FragmentModule;
import com.cloud.wms.widget.dialog.AlertDialogBuilder;
import com.dylanc.loadinghelper.LoadingHelper;
import com.trello.rxlifecycle3.components.support.RxFragment;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import es.dmoral.toasty.Toasty;

/**
 * Created by Tanghaibo
 * 2018/3/23
 * description:Fragment基类
 */

public abstract class BaseFragment<P extends BasePresenter> extends RxFragment implements BaseView {
    @Inject
    protected P mPresenter;

    protected View mView;

    protected Activity mActivity;

    protected Context mContext;

    private Unbinder unbinder;

    protected boolean isInited = false;

    private LoadingDialog mDialog;

    protected LoadingHelper loadingHelper;

    @Override
    public void onAttach(Activity activity) {
        mActivity = activity;
        mContext = activity;
        super.onAttach(activity);
    }

    protected FragmentComponent getFragmentComponent() {
        return DaggerFragmentComponent.builder()
                .applicationComponent(FineExApplication.component())
                .fragmentModule(new FragmentModule(this))
                .build();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(getResId(), null);
        setupComponent();
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mPresenter.attachView(this);
        unbinder = ButterKnife.bind(this, mView);
        if (isUseEventBus() && !EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        if (savedInstanceState == null) {
            if (!isHidden()) {
                isInited = true;
                initEvent();
            }
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!isInited && !hidden) {
            isInited = true;
            initEvent();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
        }
        unbinder = null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        if (isUseEventBus() && EventBus.getDefault().isRegistered(this)) {
            EventBusUtil.unregister(this);
        }
    }

    protected boolean isUseEventBus() {
        return false;
    }

    protected abstract int getResId();

    protected abstract void setupComponent();

    protected abstract void initEvent();

    @Override
    public void onAlert(String content, DialogInterface.OnClickListener listener) {
        new AlertDialogBuilder(this.getContext())
                .setTitle("提示")
                .setMessage(content)
                .setPositiveButton("确定", listener)
                .setNegativeButton("取消", null)
                .show();
    }

    @Override
    public void error(String msg) {
        Toasty.info(this.getContext(), msg).show();
    }

    @Override
    public void info(String msg) {
        Toasty.info(this.getContext(), msg).show();
    }

    @Override
    public void success(String msg) {
        Toasty.success(this.getContext(), msg).show();
    }

    protected LoadingDialog getDialog() {
        if (mDialog == null) {
            mDialog = new LoadingDialog.Builder(mContext).create();
        }
        return mDialog;
    }

    protected void dismiss() {
        if (mDialog == null) {
            return;
        }
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    public void showLoading() {
        getDialog().show();
    }

    @Override
    public void hideLoading() {
        dismiss();
    }

    @Override
    public void showError() {
        if (loadingHelper != null) {
            loadingHelper.showErrorView();
        }
    }

    @Override
    public void showContent() {
        if (loadingHelper != null) {
            loadingHelper.showContentView();
        }
    }

    @Override
    public void showEmpty() {
        if (loadingHelper != null) {
            loadingHelper.showEmptyView();
        }
    }

    @Override
    public Context provideContext() {
        return mContext;
    }
}
